package cdc.javafx;

import cdc.layout.Layout;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class JavaFX extends Application {
	private Stage homeStage;
	private Scene homeScence;
	
	private ToolBar createToolBar(BorderPane borderPane) {
		ToolBar toolbar = new ToolBar();
		 Button buttonBack=new Button("返回",new ImageView(DataTool.instance().getBackImage()));
		 Button buttonHome=new Button("主页",new ImageView(DataTool.instance().getHomeImage()));
		 Button buttonCode=new Button("代码",new ImageView(DataTool.instance().getCodeImage()));
		 
	     toolbar.getItems().add(buttonBack);
	     toolbar.getItems().add(buttonHome);
	     toolbar.getItems().add(buttonCode);
	     int size=DataTool.scenceLists.size();
			if(size>1) {
				buttonBack.setDisable(true);
			}else {
				buttonBack.setDisable(false);
				buttonBack.setStyle("-fx-font: 10 arial; -fx-base: #00ff00;");
				
			}
	     buttonBack.setOnAction(new BtnAction());
	     borderPane.setTop(toolbar);
	     return toolbar;
	}
	
	private class BtnAction implements EventHandler<ActionEvent>{
		@Override
		public void handle(ActionEvent event) {
			// TODO Auto-generated method stub
			Button b=(Button) event.getSource();
			System.out.println(b.getText());
			//stage.setScene(fatherScene);
			int size=DataTool.scenceLists.size();
			if(size>1) {
				b.setDisable(true);
				b.setStyle("-fx-font: 40 arial; -fx-base: #222222;");
			}else {
				event.consume();
				b.setDisable(false);
				b.setStyle("-fx-font: 30 arial; -fx-base: #00ff00;");
				
			}
			
		}
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		BorderPane borderPane = new BorderPane();
		FlowPane flowPane = new FlowPane(Orientation.HORIZONTAL, 4, 2);
		flowPane.setPrefWrapLength(240); // preferred wraplength
		borderPane.setPrefSize(1024, 768);
		borderPane.setCenter(flowPane);
		homeScence=new Scene(borderPane);
		DataTool.scenceLists.addFirst(homeScence);
		createToolBar(borderPane);
		homeStage=primaryStage;
		flowPane.getChildren().addAll(DataTool.instance().createButton(homeStage, homeScence, Layout.class));
		primaryStage.setScene(homeScence);
		primaryStage.getIcons().add(DataTool.instance().getImage());
		primaryStage.setTitle("JavaFx");
		primaryStage.show();
	}

	/**
	 * Java main for when running without JavaFX launcher
	 */
	public static void main(String[] args) {
		launch(args);
	}

}
